using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;

public class DmConventionSetBuilder : RelationalConventionSetBuilder
{
	private readonly ISqlGenerationHelper _sqlGenerationHelper;

	public DmConventionSetBuilder([NotNull] ProviderConventionSetBuilderDependencies dependencies, [NotNull] RelationalConventionSetBuilderDependencies relationalDependencies, [NotNull] ISqlGenerationHelper sqlGenerationHelper)
		: base(dependencies, relationalDependencies)
	{
		_sqlGenerationHelper = sqlGenerationHelper;
	}

	public override ConventionSet CreateConventionSet()
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Expected O, but got Unknown
		ConventionSet val = ((RelationalConventionSetBuilder)this).CreateConventionSet();
		DmValueGenerationStrategyConvention item = new DmValueGenerationStrategyConvention(this.Dependencies, this.RelationalDependencies);
		val.ModelInitializedConventions.Add((IModelInitializedConvention)(object)item);
		val.ModelInitializedConventions.Add((IModelInitializedConvention)new RelationalMaxIdentifierLengthConvention(128, this.Dependencies, this.RelationalDependencies));
		ValueGenerationConvention val2 = new DmValueGenerationConvention(this.Dependencies, this.RelationalDependencies);
		this.ReplaceConvention<IEntityTypeBaseTypeChangedConvention, ValueGenerationConvention>(val.EntityTypeBaseTypeChangedConventions, val2);
		this.ReplaceConvention<IEntityTypeAnnotationChangedConvention, RelationalValueGenerationConvention>(val.EntityTypeAnnotationChangedConventions, (RelationalValueGenerationConvention)val2);
		this.ReplaceConvention<IEntityTypePrimaryKeyChangedConvention, ValueGenerationConvention>(val.EntityTypePrimaryKeyChangedConventions, val2);
		this.ReplaceConvention<IForeignKeyAddedConvention, ValueGenerationConvention>(val.ForeignKeyAddedConventions, val2);
		this.ReplaceConvention<IForeignKeyRemovedConvention, ValueGenerationConvention>(val.ForeignKeyRemovedConventions, val2);
		StoreGenerationConvention val3 = new DmStoreGenerationConvention(this.Dependencies, this.RelationalDependencies);
		this.ReplaceConvention<IPropertyAnnotationChangedConvention, StoreGenerationConvention>(val.PropertyAnnotationChangedConventions, val3);
		this.ReplaceConvention<IPropertyAnnotationChangedConvention, RelationalValueGenerationConvention>(val.PropertyAnnotationChangedConventions, (RelationalValueGenerationConvention)val2);
		val.ModelFinalizingConventions.Add((IModelFinalizingConvention)(object)item);
		this.ReplaceConvention<IModelFinalizingConvention, StoreGenerationConvention>(val.ModelFinalizingConventions, val3);
		return val;
	}

	public static ConventionSet Build()
	{
		using IServiceScope serviceScope = ServiceProviderServiceExtensions.GetRequiredService<IServiceScopeFactory>(EntityFrameworkServiceCollectionExtensions.AddDbContext<DbContext>(new ServiceCollection().AddEntityFrameworkDm(), (Action<IServiceProvider, DbContextOptionsBuilder>)delegate(IServiceProvider p, DbContextOptionsBuilder o)
		{
			o.UseDm("Server=.").UseInternalServiceProvider(p);
		}, ServiceLifetime.Scoped, ServiceLifetime.Scoped).BuildServiceProvider()).CreateScope();
		DbContext service = ServiceProviderServiceExtensions.GetService<DbContext>(serviceScope.ServiceProvider);
		try
		{
			return ConventionSet.CreateConventionSet(service);
		}
		finally
		{
			((IDisposable)service)?.Dispose();
		}
	}
}
